<template>
  <div class="app-container">
    <el-card>
      <el-form :inline="true" :model="infoForm" class="demo-form-inline">
        <el-row :gutter="60">
          <el-col :span="8">
            <el-form-item label="员工账号:">
              <el-input
                v-model.trim="infoForm.employeeNumber"
                clearable
                placeholder="请输入员工账号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="员工姓名:">
              <el-input
                v-model.trim="infoForm.name"
                clearable
                placeholder="请输入员工姓名"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工作模式:">
              <el-select
                v-model="infoForm.workPatternId"
                clearable
                style="width: 273px"
                placeholder="请选择工作模式"
              >
                <el-option
                  v-for="item in workPatterns"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="row_2" :gutter="60">
          <el-col :span="8">
            <el-form-item label="月份:">
              <el-date-picker
                v-model="infoForm.month"
                type="month"
                placeholder="请选择择月份"
                style="width: 273px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所属机构:">
              <tree-select
                :id.sync="infoForm.agencyId"
                v-model="infoForm.value"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-button type="primary" @click="onSearch">搜索</el-button>
            <el-button @click="fnReset">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="body">
      <el-button
        type="primary"
      ><router-link
        to="/user/workArrange-setting"
      >排班设置</router-link></el-button>
      <el-button
        type="primary"
        plain
        @click="btnRelation"
      >绑定排班</el-button>
      <el-table
        ref="multipleTable"
        v-loading="loading"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column fixed type="selection" width="60" />
        <el-table-column
          fixed
          prop="employeeNumber"
          label="员工账号"
          width="100"
        />
        <el-table-column fixed prop="name" label="员工姓名" width="120" />
        <el-table-column fixed label="员工电话" prop="phone" width="120" />
        <el-table-column fixed label="员工角色" :width="(!tableData.length && '719') ||'100'">
          <template #default="{ row }">
            <span v-if="row.userType === 1">员工</span>
            <span v-else-if="row.userType === 2">快递员</span>
            <span v-else>司机</span>
          </template>
        </el-table-column>
        <el-table-column
          fixed
          prop="workPatternName"
          label="当前工作模式"
          width="110"
        />
        <el-table-column
          v-for="item in days"
          :key="item"
          :label="item + ''"
          width="80"
        >
          <template #default="{ row: { workSchedules } }">
            <div v-if="workSchedules[item - 1]" class="isRest">上</div>
            <div v-else class="isRest rest">休</div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="infoForm.page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="infoForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <!-- 人工调整弹框 -->
    <el-dialog title="人工调整" :visible.sync="dialogVisible" width="35%">
      <el-form>
        <el-form-item prop="resource">
          <el-radio-group v-model="relaWorkPattern.patterns" @input="patternsChange">
            <el-radio label="礼拜制" />
            <el-radio label="连续制" />
          </el-radio-group>
        </el-form-item>
        <el-form-item label="模式名称" prop="resource">
          <el-select ref="reference" v-model="relaWorkPattern.workPatternId">
            <template v-if="relaWorkPattern.patterns==='礼拜制'">
              <el-option v-for="item in liturgy" :key="item.id" :value="item.id" :label="item.name" />
            </template>
            <template v-else>
              <el-option v-for="item in continuous" :key="item.id" :value="item.id" :label="item.name" />
            </template>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="relationWorkPattern"
        >确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <Footer />
  </div>
</template>

<script>
import { getWorkdayAPI, getWorkPatternsAPI, relationWorkPatternAPI } from '@/api/employee'
export default {
  name: 'CourierManage',
  data() {
    return {
      tableData: [],
      infoForm: {
        page: 1,
        pageSize: 10,
        employeeNumber: '',
        name: '',
        month: '',
        agencyId: '',
        value: '',
        workPatternId: ''
      },
      counts: 0,
      loading: false,
      multipleSelection: [],
      workPatterns: [],
      dialogVisible: false,
      relaWorkPattern: {
        patterns: '礼拜制',
        workPatternId: ''
      },
      liturgy: [], // 礼拜制工作模式列表
      continuous: [] // 连续制工作模式列表
    }
  },
  computed: {
    days() {
      let days = null
      if (this.tableData.length) {
        days = this.tableData[0].workSchedules.length
      }
      return days
    }
  },
  created() {
    this.getWorkday()
    this.getWorkPatterns()
  },
  methods: {
    fnClose() {
      this.dialogVisible = false
      this.relaWorkPattern.patterns = '礼拜制'
      this.relaWorkPattern.workPatternId = ''
      this.getWorkday()
    },
    // 关联排班
    async relationWorkPattern() {
      const userIdList = this.multipleSelection.map(item => item.id)
      await relationWorkPatternAPI({
        workPatternType: this.relaWorkPattern.patterns === '礼拜制' ? '1' : '2',
        workPatternId: this.relaWorkPattern.workPatternId,
        userIdList
      })
      this.$message.success('绑定成功')
      this.fnClose()
    },
    btnRelation() {
      if (!this.multipleSelection.length) {
        this.$message.error('请选择调整人员')
      } else {
        this.dialogVisible = true
      }
    },
    // 选项切换
    patternsChange() {
      this.relaWorkPattern.workPatternId = ''
    },
    async getWorkday() {
      this.loading = true
      const res = await getWorkdayAPI({ ...this.infoForm })
      this.counts = res.counts * 1
      this.tableData = res.items
      this.loading = false
    },
    async getWorkPatterns() {
      const res = await getWorkPatternsAPI()
      for (const key in res) {
        this.workPatterns.push(...res[key])
      }
      this.liturgy = res[1]
      this.continuous = res[2]
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      this.infoForm.pageSize = val
      this.getWorkday()
    },
    handleCurrentChange(val) {
      this.infoForm.page = val
      this.getWorkday()
    },
    onSearch() {
      this.infoForm.page = 1
      this.getWorkday()
    },
    fnReset() {
      this.infoForm = {
        page: 1,
        pageSize: 10,
        employeeNumber: '',
        name: '',
        month: '',
        agencyId: '',
        value: '',
        workPatternId: ''
      }
      this.getWorkday()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  :deep(.el-card__body) {
    padding: 0;
    .el-form {
      background-color: #fff;
      min-width: 1286px;
      padding: 20px 30px;
      .el-form-item {
        display: flex;
        width: 100%;
        justify-content: flex-end;
        .el-form-item__label {
          font-weight: normal;
        }
        .el-form-item__label {
          min-width: 95px;
        }
        .el-form-item__content {
          flex: 1;
          .el-input {
            width: 267px;
          }
        }
      }
    }
    .el-button {
      width: 90px;
      margin-right: 16px;
    }
    .row_2 {
      .el-form-item {
        margin-bottom: 0;
      }
    }
  }
  .body {
    margin-top: 20px;
    .el-button {
      width: 100px;
      margin-right: 16px;
    }
    :deep(.el-card__body) {
      padding: 30px !important;
    }
    :deep(.el-table) {
      margin-top: 20px;
      border: 1px solid #ebeef5;
      a {
        color: #419eff;
        font-size: 13px;
      }
      .el-table__header th {
        background-color: #f8faff;
        &:nth-child(1) {
          .cell {
            padding-left: 20px;
          }
        }
      }
      .el-table__row td {
        &:nth-child(1) {
          .cell {
            padding-left: 20px;
          }
        }
      }
      .el-table__body-wrapper {
        &::-webkit-scrollbar {
          background-color: transparent;
        }
        &::-webkit-scrollbar-thumb {
          border-radius: 10px;
          border: 4px solid #fff;
          background-color: #dddee0;
        }
      }
      .workStatus {
        font-size: 13px;
        &::before {
          content: "";
          display: inline-block;
          margin-right: 6px;
          width: 6px;
          height: 6px;
          border-radius: 50%;
        }
      }
      .isRest {
        width: 24px;
        height: 24px;
        border-radius: 50%;
        text-align: center;
        background-color: #ffeeeb;
        border: 1px solid #e15638;
        font-size: 13px;
        color: #e15638;
      }
      .rest {
        background-color: #eff3f8;
        border: 1px solid #d8dde3;
        color: #000;
      }
    }
    .el-pagination {
      margin: 22px auto;
      text-align: center;
      span {
        font-size: 14px !important;
        color: #20232a !important;
      }
    }
  }
  :deep(.el-dialog) {
    border-radius: 8px;
    overflow: hidden;
    .el-dialog__header {
      background-color: #f3f4f7;
    }
    .el-dialog__footer {
      text-align: center;
    }
    .el-form {
      margin: 0 auto;
      display: flex;
      justify-content: space-between;
      flex-direction: column;
      flex-wrap: wrap;
      align-content: space-around;
      .el-form-item {
        width: 70%;
        display: flex;
        .el-form-item__content{
          flex-grow: 1;
          .el-select{
            width: 100%;
          }
        }
      }
    }
  }
}
</style>
